package com.zhdl.modules.prefiresys.web.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson2.JSON;
import com.zhdl.common.util.WebResult;
import com.zhdl.common.ytools.HttpClientUtil;
import com.zhdl.modules.prefiresys.entity.FactoryLocalHost;
import com.zhdl.modules.prefiresys.service.FactoryLocalHostService;
import com.zhdl.modules.prefiresys.web.request.LocalHostInsertRequest;
import com.zhdl.modules.prefiresys.web.request.LocalHostUpdateRequest;
import com.zhdl.modules.prefiresys.web.response.AllHostListResp;
import com.zhdl.modules.prefiresys.web.service.CollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 探测器装置相关接口
 */
@RestController
@RequestMapping("/api/v1/host")
public class HostController {

    @Autowired
    FactoryLocalHostService factoryLocalHostService;

    @Autowired
    CollectService collectService;

    /**
     * 查询列表
     *
     * @return
     */
    @PostMapping(path = "/list")
    public WebResult<List<AllHostListResp>> getList() {
        List<AllHostListResp> list = factoryLocalHostService.getAllHostlist();
        return WebResult.okResult(list);
    }

    /**
     * 添加
     *
     * @param request
     * @return
     */
    @PostMapping(path = "insert")
    public WebResult<FactoryLocalHost> insert(@RequestBody @Validated LocalHostInsertRequest request) {
        FactoryLocalHost entity = new FactoryLocalHost();
        BeanUtil.copyProperties(request, entity, CopyOptions.create().setIgnoreNullValue(true));
        entity.setId(IdUtil.fastSimpleUUID());
        FactoryLocalHost newDto = factoryLocalHostService.insertHost(entity);

        // 更新内存信息
        collectService.BuildMemDev();

        return WebResult.okResult(newDto);
    }

    /**
     * 编辑
     *
     * @param request
     * @return
     */
    @PostMapping(path = "update")
    public WebResult<FactoryLocalHost> update(@RequestBody @Validated LocalHostUpdateRequest request) {
        FactoryLocalHost entity = new FactoryLocalHost();
        BeanUtil.copyProperties(request, entity, CopyOptions.create().setIgnoreNullValue(true));
        FactoryLocalHost newDto = factoryLocalHostService.updateHost(entity);
        //更新内存信息
        collectService.BuildMemDev();
        //6-10
        System.out.println("request.getProjectId():" + request.getProjectId());//6-9
        String jsonString = JSON.toJSONString(newDto);
        String urlPath = new String("/api/baseLocalHost/addBaseLocalHost");
        String returnResult = HttpClientUtil.reportDataByHttp(jsonString, urlPath);

        return WebResult.okResult(newDto);
    }


    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping(path = "delete/{id}")
    public WebResult deleteById(@PathVariable("id") String id) {

        factoryLocalHostService.deleteById(id);

        // 更新内存信息
        collectService.BuildMemDev();

        return WebResult.okResult();
    }

    /**
     * 根据ID查询某一个
     *
     * @param id
     * @return
     */
    @GetMapping(path = "/getById/{id}")
    public WebResult<FactoryLocalHost> selectById(@PathVariable String id) {
        FactoryLocalHost comSp = factoryLocalHostService.selectById(id);
        return WebResult.okResult(comSp);
    }


}
